package panda.query.aggr;

import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;
import panda.query.struct.IntConstant;
import panda.query.struct.NullConstant;
import panda.query.struct.SemAttribute;

/**
 * 
 * @author Tian Yuan
 */
public class IntSumFn implements AggregateFn {
	int total;
	Attribute attr;
	Attribute nattr;
	
	public IntSumFn(Attribute attr, Attribute nattr) {
		this.attr = attr;
		this.nattr = nattr;
	}
	
	public void first(Scanner s) {
		Constant x = s.getValue(attr);
		if (x instanceof NullConstant) return;
		
		total = (Integer)((IntConstant)x).getContentValue();
	}
	
	public void next(Scanner s) {
		Constant x = s.getValue(attr);
		if (x instanceof NullConstant) return;
		
		total += (Integer)((IntConstant)x).getContentValue();
	}

	public Attribute getAttribute() {
		return nattr;
	}

	public Constant getValue() {
		return new IntConstant(total);
	}

}
